// pages/recipeDetail/recipeDetail.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    collect:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    console.log(options.cook);
    var id = options.cookId;
    //获取手机高度
    this.setData({
      navH: app.globalData.navHeight,
      cookBookId: id,
    });
    //that.getAllUserCollection();
    wx.request({
      url: app.globalData.url + 'readCookBook',
      method: "post",
      data: {
        cookId: that.data.cookBookId,
      },
      header: {
        "content-type": "application/x-www-form-urlencoded"
      }
    });
    wx.request({
      url: app.globalData.url + 'getCookBookById',
      method: "post",
      data: {
        cookId: id,
      },
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      success:function(res){
        if (res.data.result == 1) {
          that.setData({
            cookBook: res.data.data
          });
        } 
      }
    });

    wx.request({
      url: app.globalData.url + 'judgeUserCollectionCookBook',
      method:"get",
      data:{
        userId:wx.getStorageSync("userId"),
        cookBookId: that.data.cookBookId,
      },
      success:function(res){
        if(res.data.result == 0){
          that.setData({
            collect:1
          });
        } 
      }
    })
  },

  getAllUserCollection:function(){
    wx.request({
      url: app.globalData.url + '',
      method:"get",
      data:{
        userId:wx.getStorageSync("userId"),
      },
      success:function(res){
        var list = res.data.data;
        for(var i = 0; i < list.length; i++){
          if (that.data.cookBookId == list[i].cookBookId){
            that.setData({
              collect:1,
              collectionId: list[i].collectionId
            })
          }
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  navBack: function () {
    wx.navigateBack({

    })
  },

  collection:function(e){
    var that = this;
    var type = e.currentTarget.dataset.type;
    if (type==1) {
      wx.request({
        url: app.globalData.url + 'userCollectCookBook',
        method: "post",
        data:{
          userId:wx.getStorageSync("userId"),
          cookBookId: that.data.cookBookId,
        },
        success: function (res) {
          if (res.data.result == 1) {
            that.setData({
              collect: 1
            });
            wx.showToast({
              title: '收藏成功！',
              type: "success",
            });
          } else {
            wx.showToast({
              title: '收藏失败！',
              type: "failed",
            });
          }
        }
      })
    } else {
      wx.request({
        url: app.globalData.url + 'userCancelCollectionCookBook',
        method: "post",
        data: {
          collectionId: that.data.collectionId,
        },
        success: function (res) {
          if (res.data.result == 1) {
            that.setData({
              collect: 0
            });
            wx.showToast({
              title: '取消成功！',
              type: "success",
            });
          } else {
            wx.showToast({
              title: '取消失败！',
              type: "failed",
            });
          }
        }
      })
    }
    

  },
  buy: function() {
    var productid = this.data.cookBook.productList[0].productId;
    var type = this.data.cookBook.productList[0].type;
    wx.navigateTo({
      url: '/pages/productDetail/productDetail?productid=' + productid + '&type=' + type,
    })

  },
})